<extend name="Public:base" />
<block name="content">
        <volist name="product" id="vo">
        <div class="row">
            <div class="col-xs-12">
                <form class="form-horizontal"  id="validation-form" enctype="multipart/form-data"  method="post" onsubmit="return check()" action='{:U("ProductAcc/editAcc")}'>
                    <div class="form-group required">
                        <label class="col-sm-2 control-label">产品名称：</label>
                        <div class="col-sm-10">
                            <div class="clearfix">
                                <input  class="form-control" style="width:220px;" ype="text" placeholder="" name="acc_name"  value="{$vo.name}" />
                            </div>
                        </div>
                    </div>
                    <input type="hidden" name="id" value="{$vo.id}"/>
                    <div class="form-group required">
                        <label class="col-sm-2 control-label">上传图片：</label>
                        <div class="col-sm-10" id="up">
                            <input type="file" name="icon" id="icon" onchange="preview(event)">
                            <img src="{$vo.icon}" width="145" height="155" id="img">
                        </div>
                    </div>
                    <div class="form-group required">
                        <label class="col-sm-2 control-label">产品价格：</label>
                        <div class="col-sm-10">
                            <div class="clearfix">
                                <input  class="form-control" style="width:220px;" type="text" placeholder="" name="price"  value="{$vo.price}" />
                            </div>
                        </div>
                    </div>
                    <div class="form-group required">
                        <label class="col-sm-2 control-label">分类：</label>
                        <div class="col-sm-10">
                            <select name="category_type">
                                <option>请选择类型</option>
                                <volist name="select" id="v">
                                    <php>if($vo['category_id'] == $v['category_id']){</php>
                                    <option value="{$v.category_id},{$v.path_name}" selected="selected">{$v.path_name}</option>
                                    <php>}else{</php>
                                    <option value="{$v.category_id},{$v.path_name}">{$v.path_name}</option>
                                    <php>}</php>
                                </volist>
                            </select>
                        </div>
                    </div>
                    <div class="form-group required">
                        <label class="col-sm-2 control-label">排序：</label>
                        <div class="col-sm-10">
                            <div class="clearfix">
                                <input  class="form-control" style="width:220px;" type="text" placeholder="" name="order" value="{$vo.order}"/>
                            </div>
                        </div>
                    </div>
                    <div class="form-group required">
                        <label class="col-sm-2 control-label">产品外链：</label>
                        <div class="col-sm-10">
                            <div class="clearfix">
                                <input  class="form-control" style="width:220px;" type="text" placeholder="" name="url" value="{$vo.url}"/>
                            </div>
                        </div>
                    </div>
                    <div class="form-group required">
                        <label class="col-sm-2 control-label">状态：</label>
                        <div class="col-sm-10">
                            <?php if($vo['status']==1){?>
                            <label><input type="radio"  name="status" value="1" onclick="" checked="checked"/>开启</label>&nbsp;&nbsp;&nbsp;&nbsp;
                            <label><input type="radio" name="status" value="2" onclick="" />关闭</label>
                            <?php }else{?>
                            <label><input type="radio"  name="status" value="1" onclick=""/>开启</label>&nbsp;&nbsp;&nbsp;&nbsp;
                            <label><input type="radio" name ="status" value="2" onclick=""  checked="checked"/>关闭</label>
                            <?php }?>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" style="visibility: hidden;">提交：</label>
                        <div class="col-sm-10">
                            <input type="submit" value="提交" class="btn btn-primary radius5 btn-sm">
                            <a class="btn btn-primary radius5 btn-sm"  href="{:U('ProductAcc/Product')}"> 返回 </a>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        </volist>
    </div>
</block>
<block name="javascript">
    <script src="__JS__/jquery.validate.min.js"></script>
    <script>
        function check(){
            var select = $("select").val();
            var acc_name = $("input[name=acc_name]").val();
            var price = $("input[name=price]").val();
            var order = $("input[name=order]").val();
            var url = $("input[name=url]").val();

            if($.trim(acc_name)==''){
                $.jbox.info('请输入名称！','提示')
                return false;
            }
            if($.trim(price)==''){
                $.jbox.info('价格不能为空！','提示');
                return false;
            }
            if($.trim(price)=='0'){
                $.jbox.info('价格不能为零！','提示');
                return false;
            }
            if(isNaN(price)){
                $.jbox.info('请输入数字！','提示');
                return false;
            }
            if(select=='请选择类型'){
                $.jbox.info('请选择分类！','提示');
                return false;
            }
            if($.trim(order)==''){
                $.jbox.info('排序不能为空！','提示');
                return false;
            }
            if(isNaN(order)){
                $.jbox.info('请输入数字！','提示');
                return false;
            }
            if(!url){
                $.jbox.info('请输入地址！','提示');
                return false;
            }
            if(url){
                var result = IsURL(url);
                if(!result){
                    $.jbox.info('请输入正确的地址！','提示');
                    return false;
                }
            }

            var img = document.getElementById("img");
            if(!img){
                $.jbox.info('请加入图片！','提示');
                return false;
            }
            $('#validation-form').ajaxSubmit(function(data){
                if(data=='1'){
                    $.jbox.tip("添加成功！", 'loading');
                    window.setTimeout(function () {window.location.href="{:U('ProductAcc/Accessories')}"; }, 500);
                }if(data=='2'){
                    $.jbox.info('添加失败！','提示');
                }if(data=='3'){
                    $.jbox.tip("修改成功！", 'loading');
                    window.setTimeout(function () {window.location.href="{:U('ProductAcc/Accessories')}"; }, 500);
                }
            });

            return false;
        }

        function preview(event) {
            var files = event.target.files;
            var reader  = new FileReader();

            reader.onload = function (event) {
                var result = event.target.result;

                var x = document.getElementById("icon");
                if (!x || !x.value) return;
                var patn = /\.jpg$|\.jpeg$|\.gif$|\.png$/i;
                if (patn.test(x.value)) {
                    var y = document.getElementById("img");
                    if (y) {
                        y.src = result;
                    } else {
                        var img = document.createElement("img");
                        img.setAttribute("src", result);
                        img.setAttribute("width", "145");
                        img.setAttribute("height", "155");
                        img.setAttribute("id", "img");
                        document.getElementById("up").appendChild(img);
                    }
                } else {
                    $.jbox.info("您选择的似乎不是图像文件!",'提示');
                }

                reader = null;
            };
            reader.readAsDataURL(files[0]);

        }

        function IsURL(url){
                var RegUrl = new RegExp();
                RegUrl.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
                if (!RegUrl.test(url)) {
                    return false;
                }
                return true;
        }
    </script>
</block>

